package com.blacktech.dbu.meta.controller;

import com.blacktech.dbu.core.controller.BaseCrudController;
import com.blacktech.dbu.core.dto.SearchRequest;
import com.blacktech.dbu.core.exception.BusinessException;
import com.blacktech.dbu.core.response.PageResponse;
import com.blacktech.dbu.core.response.ResponseCode;
import com.blacktech.dbu.meta.entity.DbuProduct;
import com.blacktech.dbu.meta.repository.ProductRepository;
import com.blacktech.dbu.meta.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品管理Controller
 * 提供产品的完整CRUD操作和搜索功能
 *
 * @author Yanyunsong
 */
@RestController
@RequestMapping("/api/products")
@Tag(name = "产品管理", description = "产品信息的增删改查和搜索")
public class ProductController extends BaseCrudController<DbuProduct, Long> {

    private final ProductRepository productRepository;
    private final ProductService productService;

    @Autowired
    public ProductController(ProductRepository productRepository, ProductService productService) {
        this.productRepository = productRepository;
        this.productService = productService;
    }

    @Override
    protected DbuProduct doCreate(DbuProduct entity) {
        return productService.createProduct(entity);
    }

    @Override
    protected DbuProduct doGetById(Long id) {
        return productRepository.findById(id).orElse(null);
    }

    @Override
    protected Page<DbuProduct> doGetAll(Pageable pageable) {
        return productRepository.findAll(pageable);
    }

    @Override
    protected List<DbuProduct> doGetAllWithoutPagination() {
        return productRepository.findAll();
    }

    @Override
    protected DbuProduct doUpdate(Long id, DbuProduct entity) {
        return productService.updateProduct(id, entity);
    }

    @Override
    protected boolean doDelete(Long id) {
        if (!productRepository.existsById(id)) {
            return false;
        }
        productRepository.deleteById(id);
        return true;
    }

    @Override
    protected int doBatchDelete(List<Long> ids) {
        return productService.batchDeleteProducts(ids);
    }

    @Override
    protected Page<DbuProduct> doSearch(Specification<DbuProduct> specification, Pageable pageable) {
        return productRepository.findAll(specification, pageable);
    }

    @Override
    protected Class<DbuProduct> getEntityClass() {
        return DbuProduct.class;
    }

    /**
     * 根据产品代号查找产品
     */
    @GetMapping("/code/{productCode}")
    @Operation(summary = "根据产品代号查找产品", description = "通过产品代号查找产品信息")
    public DbuProduct getByProductCode(
            @Parameter(description = "产品代号") @PathVariable String productCode) {
        return productRepository.findByProductCodeIgnoreCase(productCode)
                .orElseThrow(() -> new BusinessException(
                        ResponseCode.NOT_FOUND.getCode(),
                    "产品不存在"
                ));
    }

    /**
     * 检查产品代号是否存在
     */
    @GetMapping("/check-code/{productCode}")
    @Operation(summary = "检查产品代号是否存在", description = "检查指定产品代号是否已被使用")
    public Boolean checkProductCodeExists(
            @Parameter(description = "产品代号") @PathVariable String productCode) {
        return productRepository.existsByProductCodeIgnoreCase(productCode);
    }
}